/**
 * 
 */
package image;

import codec.JPEGCodec;
import util.Log;
import image.header.JPEGHeader;

/**
 * @author nquangtrung
 *
 */
public class JPEGImage implements Image {
	public static final String TAG = "JPEGImage";
	private byte[] jpegscan;
	private JPEGHeader header;
	
	public static JPEGImage makeJPEGImage(Image image, JPEGHeader header) {
		JPEGCodec codec = new JPEGCodec(image, header);
		byte[] jpegscan = codec.makeJPEGData();
		
		JPEGImage jpeg = new JPEGImage();
		jpeg.setHeader(header);
		jpeg.setJPEGScan(jpegscan);
		
		return jpeg;
	}

	/**
	 * 
	 */
	private JPEGImage() { }
	
	public byte[] getFullFile() {
		byte[] header = this.getHeader().getByte();
		byte[] scan = this.getJPEGScan();
		int size = header.length + scan.length;
		byte[] b = new byte[size + 2];
		// System.out.printf("%4x %4x\n", header.length, scan.length);
		for (int i = 0; i < size; i++) {
			if (i < header.length) {
				b[i] = header[i];
			} else {
				b[i] = scan[i - header.length];
			}
		}
		b[size] = (byte)0xFF;
		b[size + 1] = JPEGHeader.SEG_END_OF_IMAGE;
		Log.debugArray(TAG, "Header: ", header, 9);
		Log.debugArray(TAG, "JPEGScan: ", scan, 9);
		return b;
	}
	
	public void setWidth(int width) {
		this.getHeader().setWidth(width);
	}
	public void setHeight(int height) {
		this.getHeader().setHeight(height);
	}
	
	public JPEGImage(byte[] scan, double quality) {
		this.setJPEGScan(scan);
		this.setHeader(new JPEGHeader());
		this.setQuality(quality);
	}

	/* (non-Javadoc)
	 * @see image.Image#getRGB(int, int)
	 */
	@Override
	public int getRGB(int x, int y) {
		// TODO Auto-generated method stub
		return 0;
	}

	/* (non-Javadoc)
	 * @see image.Image#getRed(int, int)
	 */
	@Override
	public int getRed(int x, int y) {
		// TODO Auto-generated method stub
		return 0;
	}

	/* (non-Javadoc)
	 * @see image.Image#getBlue(int, int)
	 */
	@Override
	public int getBlue(int x, int y) {
		// TODO Auto-generated method stub
		return 0;
	}

	/* (non-Javadoc)
	 * @see image.Image#getGreen(int, int)
	 */
	@Override
	public int getGreen(int x, int y) {
		// TODO Auto-generated method stub
		return 0;
	}

	/* (non-Javadoc)
	 * @see image.Image#getWidth()
	 */
	@Override
	public int getWidth() {
		return this.getHeader().getWidth();
	}
	/* (non-Javadoc)
	 * @see image.Image#getHeight()
	 */
	@Override
	public int getHeight() {
		return this.getHeader().getHeight();
	}

	/**
	 * @return the jpegscan
	 */
	public byte[] getJPEGScan() {
		return jpegscan;
	}

	/**
	 * @param jpegscan the jpegscan to set
	 */
	public void setJPEGScan(byte[] jpegscan) {
		this.jpegscan = jpegscan;
	}

	/**
	 * @return the header
	 */
	public JPEGHeader getHeader() {
		return header;
	}

	/**
	 * @param header the header to set
	 */
	public void setHeader(JPEGHeader header) {
		this.header = header;
	}

	/**
	 * @return the quality
	 */
	public double getQuality() {
		return header.getQuality();
	}

	/**
	 * @param quality the quality to set
	 */
	public void setQuality(double quality) {
		header.setQuality(quality);
	}

}
